λ¬Έμ
π Logic
public int rideBus() {
int departBus = 540;
for (int i = 0; i < n; i++) {
while(!crueTimes.isEmpty()) {
int crue = crueTimes.peek();
if (departBus >= crue && busTimes.get(i).size() < m) {
crueTimes.poll();
busTimes.get(i).add(crue);
lastCrue = crue - 1;
} else break;
}
departBus += t;
}
if (busTimes.get(n - 1).size() < m)
lastCrue = departBus - t;
return lastCrue;
}
departBusλ‘ λ²μ€ μΆλ° μκ°μ μΈν νκ³ , μ°μ μμ νμ μ μ₯λ ν¬λ£¨λ€μ λκΈ°μ΄ λμ°© μκ°μ νλμ© λΉκ΅νλ©° νμΉν μ μλ κ²½μ° νμμ μ μΈνλ€.- μ΄λ λ§μ§λ§μΌλ‘ νμΉν μΈμμ λκΈ°μ΄ λμ°© μκ°μ μμμΌ νκΈ°μ
lastCrueμ μ μ₯νλ€. (μ΄λ -1ν κ°μ μ μ₯) -
μ½μ λ§μ§λ§ λ²μ€μλ§ νλ©΄ λλ―λ‘, λ§μ§λ§ λ²μ€κ° λ§μμΈμ§ νμΈνμ¬
- λ§μμΈ κ²½μ°,
lastCrueλ₯Ό λ°ννκ³ , - λ§μμ΄ μλ κ²½μ°, ν΄λΉ λ²μ€μ μΆλ° μκ°μ λ°ννλ€.
- λ§μμΈ κ²½μ°,
π CODE
μ½λ 보기/μ κΈ°π«
import java.util.*;
class Shuttle {
Queue<Integer> crueTimes;
ArrayList<ArrayList<Integer>> busTimes;
int n;
int t;
int m;
int lastCrue;
public Shuttle(int n, int t, int m) {
this.n = n;
this.t = t;
this.m = m;
busTimes = new ArrayList<>();
crueTimes = new PriorityQueue<>();
initBusTimes();
}
private void initBusTimes() {
for (int i = 0; i < n; i++)
busTimes.add(new ArrayList<>());
}
public void fillCrue(String[] timetable) {
for (String tt : timetable) {
String[] tmp = tt.split(":");
crueTimes.add(Integer.parseInt(tmp[0]) * 60 + Integer.parseInt(tmp[1]));
}
}
public int rideBus() {
int departBus = 540;
for (int i = 0; i < n; i++) {
while(!crueTimes.isEmpty()) {
int crue = crueTimes.peek();
if (departBus >= crue && busTimes.get(i).size() < m) {
crueTimes.poll();
busTimes.get(i).add(crue);
lastCrue = crue - 1;
} else break;
}
departBus += t;
}
if (busTimes.get(n - 1).size() < m)
lastCrue = departBus - t;
return lastCrue;
}
}
class Solution {
public String solution(int n, int t, int m, String[] timetable) {
Shuttle st = new Shuttle(n, t, m);
st.fillCrue(timetable);
StringBuilder answer = new StringBuilder();
int time = st.rideBus();
int hour = time/60;
int min = time%60;
if (hour < 10) answer.append("0");
answer.append(hour).append(":");
if (min < 10) answer.append("0");
answer.append(min);
return answer.toString();
}
}
βοΈ Review
- λ²μ€λ§λ€ ν μ μλ μΈμμ λͺ¨λ μ±μλκ³ , 맨 λ§μ§λ§ λ²μ€μ λν΄μλ§ νμΈνμ¬ μ μΌ λ¦μ μκ°μ ꡬνλ €κ³ νλλ°, ν μΌ 14 ~ 16λ²μ΄ μ£½μ΄λ μ€ν¨μλ€.
- μκ°λλ ν μΌλ λ€ λ§λ€μ΄μ ν΄λ΄λ λμ ν λκ° μ λ§λ κ±΄μ§ λͺ¨λ₯΄κ² μ΄μ λ€λ₯Έ νμ΄λ₯Ό λ΄€λ€.
- λκ° μ’ μμμνκ² μλ €μ€¬μΌλ©΄ μ’κ² λ€ ...